//
// Created by 33145 on 2023/4/18.
//

#include "mysql_connection.h"

namespace sylar {
  namespace sql {

    static sylar::Logger::ptr g_logger = SYLAR_LOG_NAME("system");

    MySQL::MySQL(const std::string& user, const std::string& passWd , const std::string& databaseName,
                 const std::string& url, const std::string& port)
                 : m_url(url), m_port(port), m_user(user), m_passWord(passWd), m_databaseName(databaseName) {}

    bool MySQL::connect() {
      //MYSQL *mysql_real_connect(MYSQL *mysql, const char *host,
      //const char *user, const char *passwd,
      //const char *db, unsigned int port,
      //const char *unix_socket,

      if (m_mysql && !m_haserror) {
        m_haserror = true;
        return false;
      }
      m_haserror = false;
      m_mysql = std::make_shared<MYSQL>();
      MYSQL* m = mysql_init(m_mysql.get());
      if (!m) {
        m_haserror = true;
        SYLAR_LOG_ERROR(g_logger) << "execute error: " << mysql_error(m_mysql.get());
        return false;
      }
      // std::cout << m_url.c_str() << std::endl;
      // std::cout << m_user.c_str() << std::endl;
      // std::cout << m_passWord.c_str() << std::endl;
      // std::cout << m_databaseName.c_str() << std::endl;
      if (!::mysql_real_connect(m_mysql.get(),
           m_url.c_str(), m_user.c_str(), m_passWord.c_str(), m_databaseName.c_str(), 3306, nullptr, 0)) {
        m_haserror = true;
        SYLAR_LOG_ERROR(g_logger) << "execute error: " << mysql_error(m_mysql.get());
        return false;
      }
      return true;
    }

    bool MySQL::ping() {
        if(!m_mysql) {
          return false;
        }
        if(mysql_ping(m_mysql.get())) {
          m_haserror = true;
          return false;
        }
      m_haserror = false;
        return true;
    }

    int MySQL::execute(const std::string &sql) {
      int res = ::mysql_query(m_mysql.get(), sql.c_str());
      if (res) {
        SYLAR_LOG_ERROR(g_logger) << "execute error: " << mysql_error(m_mysql.get());
        m_haserror = true;
      } else {
        m_haserror = false;
      }
      return res;
    }

    std::shared_ptr<MYSQL> MySQL::getMySQL() {
      return m_mysql;
    }

    MYSQL_RES *MySQL::my_mysql_query(MySQL::ptr mysql, const char* sql) {
      if(mysql == nullptr) {
        SYLAR_LOG_ERROR(g_logger) << "mysql_query mysql is null";
        return nullptr;
      }

      if(sql == nullptr) {
        SYLAR_LOG_ERROR(g_logger) << "mysql_query sql is null";
        return nullptr;
      }

      if(::mysql_query(mysql->getMySQL().get(), sql)) {
        SYLAR_LOG_ERROR(g_logger) << "mysql_query(" << sql << ") error:"
                                  << mysql_error(mysql->getMySQL().get());
        return nullptr;
      }

      MYSQL_RES* res = mysql_store_result(mysql->getMySQL().get());
      if(res == nullptr) {
        SYLAR_LOG_ERROR(g_logger) << "mysql_store_result() error:"
                                  << mysql_error(mysql->getMySQL().get());
      }
      return res;
    }


  }
}